import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class Compute  extends JFrame implements  ActionListener {//委托 ActionListener
    JTextField num1 = new JTextField(7);
    JTextField num2 = new JTextField(7);
    JTextField ret = new JTextField(7);

    JButton add = new JButton("=");

    public Compute() {
        this.setSize(400, 150);
        this.setTitle("Computer");

        Container c = this.getContentPane();
        c.setLayout(new FlowLayout());

        add.addActionListener(this);


        c.add(num1);
        c.add(new JLabel("+"));
        c.add(num2);
        c.add(add);
        c.add(ret);
        ret.setEditable(false);

       //add.addActionListener(this);//注册监听
        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int n1 = Integer.parseInt(num1.getText());
                int n2 = Integer.parseInt(num2.getText());

                ret.setText(String.valueOf(n1+n2));

            }
        });

        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {

        new Compute();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

     //  if(e.getSource()==?) System.out.println("hello");//要实现什么样的监听效果
     //   if(e.getSource()==?) System.out.println("jjj");
    }

//    @Override
//    public void actionPerformed(ActionEvent e) {
//            int n1 = Integer.parseInt(num1.getText());
//            int n2 = Integer.parseInt(num2.getText());
//            ret.setText(String.valueOf(n1 + n2));
//        }
}
